#include <iostream>
#include <string>
#include "Log.hpp"

using namespace std;

class Task
{
public:
    Task(int sockfd, const std::string &clientip, const uint16_t &clientport)
        : _sockfd(sockfd),_ip(clientip), _port(clientport)
    {
    }

    void run()
    {
        char buffer[1024];
        // cout<<"quickly write"<<endl;
        //read函数还会阻塞等待吗？？？
        int n = read(_sockfd, buffer, sizeof(buffer));
        if (n > 0)
        {
            buffer[n] = '\0';
            string message = "client say# ";
            message += buffer;

            n = write(_sockfd, message.c_str(), message.size());
            //返回值小于0，意味着向一个不存在的文件描述符进行了写入
            if(n < 0)
            {
                lg(Warning,"server write to client fail,errno:%d,message:%s",errno,strerror(errno));
            }
        }
        else if (n == 0)
        {
            lg(Info, "%s:%d,has exited\n", _ip.c_str(), _port);
        }
        else if (n < 0)
        {
            lg(Warning, "Read Nothing form %s:%d\n", _ip.c_str(), _port);
        }

        close(_sockfd);
    }

private:
    int _sockfd;
    uint16_t _port;
    string _ip;
};